package com.yc.txx.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.bean.Result;
import com.yc.txx.entity.TxxCar;
import com.yc.txx.mapper.TxxCarMapper;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 天下行租车-车辆表-10 前端控制器
 * </p>
 *
 * @author k
 * @since 2025-02-04
 */
@RestController
@RequestMapping("/txxcar")
public class TxxCarController {
    @Autowired
    TxxCarMapper txxCarMapper;
    @GetMapping("getBrands")
    public Result getBrands() {
        String[] brands = txxCarMapper.getBrands();
        return new Result(1,"查询成功",brands);
    }
    @GetMapping("getCars")
    public Result getCars(@RequestParam("current") Integer current,
                          @RequestParam("size")  Integer size,
                          String name,
                          String brands) {
        LambdaQueryWrapper<TxxCar> lq = new LambdaQueryWrapper<>();
        if(!name.equals("")){
            lq.like(TxxCar::getBrand,name).or().like(TxxCar::getModel,name);
        }
        // 按品牌筛选
        if (brands != null && !brands.isEmpty()) {
            List<String> brandList = Arrays.asList(brands.split(",")); // 将 brands 字符串转换为列表
            lq.in(TxxCar::getBrand, brandList); // 使用 in 方法查询多个品牌
        }
        lq.eq(TxxCar::getState,"在库");
        Page pages = txxCarMapper.selectPage(new Page<>(current,size), lq);
        Map map = new HashMap();
        map.put("total",pages.getTotal());
        map.put("records",pages.getRecords());
        return new Result(1,"获取车辆数据成功",map);
    }
    @GetMapping("getCarById")
    public Result getCarById(@RequestParam("id") Integer id) {
        TxxCar car = txxCarMapper.selectById(id);
        if(car == null) {
            return new Result(-1,"车辆详细查询失败",null);
        }
        return new Result(1,"获取车辆信息成功",car);
    }
}
